/*
 * AutismSiteMasterController.java
 *
 * Created on October 24, 2006, 3:59 PM
 */

package com.asaga.controller;

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;

import java.util.*;

import com.asaga.tools.*;

/**
 *
 * @author jeremys
 * @version
 */
public class AutismSiteMasterController extends HttpServlet {
    
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        HttpSession session = request.getSession();
        String action = request.getParameter("action");
        String submit = request.getParameter("submit");
        String message = null;
        SystemClient_DBImpl scdb = null;
        SystemClient_DBImpl userProfile = null;
        AutismValidationHandler avh = new AutismValidationHandler(request, session);
        
        
        /* Original Author: Jeremy
         * Last Modified by: Jeremy, 2006-12-03
         * Comments: This block handles session and user authentication.  The
         *              user is either authenticated to the portal, registered,
         *              or session is verified for any other action.        
         */
         if(action==null){
            response.sendRedirect("login.jsp");
         }
         else if (action.equals("register")){
            RegistrationHandler rh = new RegistrationHandler(request);
            if(rh.RegisterPerson()==false){
                message = rh.getErrorMessage();
                scdb = rh.getRegistrationInfo();
                session.setAttribute("errorMessage", message);
                session.setAttribute("clientInfo",scdb);
                RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/register.jsp");         
                dispatcher.forward(request,response);
            }
            else{
                message = rh.getErrorMessage();
                session.setAttribute("errorMessage", message);
                session.setAttribute("registration_handler",rh);           
                RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/confirmation.jsp");         
                dispatcher.forward(request,response);
            }
        }
        else if(action.equals("login")){
            
            
            boolean sessionflag = false;           
            if(!avh.makeUserSession(request,session)){
                RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/login.jsp?message=Incorrect Username Or Password");
                dispatcher.forward(request,response);
            }else{
                RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/portal.jsp");
                dispatcher.forward(request,response);                
            }
            
        }else{  // If user is not logging in or registering, check session and do everything else.
            
            if(!avh.checkUserSession(session,response)){
                //session.invalidate();
                response.sendRedirect("login.jsp");
            } 
                   
            /* Original Author: Jeremy
             * Last Modified by: Jeremy, 2006-12-03
             * Comments: This is the master controller/redirector     
             */
            else if(action.equals("logoff")){
                session.removeAttribute("username");
                session.removeAttribute("userProfile");
                response.sendRedirect("login.jsp");
            }
            else if(action.equals("portal")){
                RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/portal.jsp");
                dispatcher.forward(request,response);            
            }
            /************************
             * Event options
             ************************/
            else if(action.equals("calendar")){
                userProfile = (SystemClient_DBImpl)session.getAttribute("userProfile");
                userProfile.setErrmsg("");
                userProfile.setWelcomeMsg("");
                session.setAttribute("userProfile", userProfile);
                response.sendRedirect("calendar.jsp");
            }
            else if(action.equals("Create Event")){
                userProfile = (SystemClient_DBImpl)session.getAttribute("userProfile");
                userProfile.setErrmsg("");
                userProfile.setWelcomeMsg("");
                session.setAttribute("userProfile", userProfile);
                response.sendRedirect("addevent.jsp");
            }
            
            else if(action.equals("viewAllEvents")){
                userProfile = (SystemClient_DBImpl)session.getAttribute("userProfile");
                userProfile.setErrmsg("");
                userProfile.setWelcomeMsg("");
                session.setAttribute("userProfile", userProfile);
                response.sendRedirect("allevents.jsp");
            }            
            else if(action.equals("View Event")){
                userProfile = (SystemClient_DBImpl)session.getAttribute("userProfile");
                userProfile.setErrmsg("");
                userProfile.setWelcomeMsg("");
                session.setAttribute("userProfile", userProfile);
                CalendarHandler eh = new CalendarHandler(request);
                eh.setEventById();
                session.setAttribute("event", eh.getEvent());
                session.setAttribute("location", eh.getEvent().getLocation());
                response.sendRedirect("viewevent.jsp");
            }
            else if(action.equals("Remove Event")){
                CalendarHandler eh = new CalendarHandler(request);
                eh.removeEvent();
                session.setAttribute("event", eh.getEvent());
                session.setAttribute("userProfile", eh.getUserProfile());
                response.sendRedirect("myevents.jsp");
            }
            else if(action.equals("listEvents")){
                userProfile = (SystemClient_DBImpl)session.getAttribute("userProfile");
                userProfile.setErrmsg("");
                userProfile.setWelcomeMsg("");
                session.setAttribute("userProfile", userProfile);
                response.sendRedirect("myevents.jsp");
            }
            else if(action.equals("addevent")){        
                userProfile = (SystemClient_DBImpl)session.getAttribute("userProfile");
                userProfile.setErrmsg("");
                userProfile.setWelcomeMsg("");
                session.setAttribute("userProfile", userProfile);
                CalendarHandler eh = new CalendarHandler(request);
                if(!eh.addEvent().equals("success")){
                    response.sendRedirect("addevent.jsp");
                    session.setAttribute("event", eh.getEvent());
                    session.setAttribute("location", eh.getLocation());
                }
                else{
                    session.setAttribute("userProfile", eh.getUserProfile());
                    session.setAttribute("event", eh.getEvent());
                    session.setAttribute("location", eh.getLocation());
                    response.sendRedirect("myevents.jsp");
                }
            }
            else if(action.equals("verifyevent")){
                CalendarHandler eh = new CalendarHandler();
                if(submit.equals("Yes")){
                    eh.setEvent((Event_DBImpl)session.getAttribute("event"));
                    eh.addEvent();
                    response.sendRedirect("portal.jsp");
                }
                else{
                    response.sendRedirect("addevent.jsp");
                }
            }
            else if(action.equals("addToCalendar")){
                CalendarHandler eh = new CalendarHandler(request);
                eh.addEventToCalendar();
                //eh.addEvent();
                session.setAttribute("event", eh.getEvent());
                session.setAttribute("userProfile", eh.getUserProfile());
                session.setAttribute("location", eh.getEvent().getLocation());
                //session.setAttribute("userProfile", scdb);
                response.sendRedirect("viewevent.jsp");
            }
            else if(action.equals("updateevent")){
                CalendarHandler eh = new CalendarHandler(request);
                eh.updateEvent();
                session.setAttribute("event", eh.getEvent());
                //session.setAttribute("userProfile", eh.getUserProfile());
                session.setAttribute("location", eh.getEvent().getLocation());
                response.sendRedirect("viewevent.jsp");
            }
            else if(action.equals("Update Event")){
                userProfile = (SystemClient_DBImpl)session.getAttribute("userProfile");
                userProfile.setErrmsg("");
                userProfile.setWelcomeMsg("");
                session.setAttribute("userProfile", userProfile);
                CalendarHandler eh = new CalendarHandler(request);
                eh.setEventById();
                session.setAttribute("event", eh.getEvent());
                session.setAttribute("location", eh.getEvent().getLocation());
                response.sendRedirect("updateevent.jsp");
            }
            
            else if(action.equals("Provide Feedback")){
                userProfile = (SystemClient_DBImpl)session.getAttribute("userProfile");
                userProfile.setErrmsg("");
                userProfile.setWelcomeMsg("");
                session.setAttribute("userProfile", userProfile);                
                CalendarHandler eh = new CalendarHandler(request);
                eh.setEventById();
                session.setAttribute("event", eh.getEvent());
                session.setAttribute("location", eh.getEvent().getLocation());
                response.sendRedirect("viewevent.jsp");                
            }
            else if(action.equals("Add Comment")){
                CalendarHandler eh = new CalendarHandler(request);
                eh.addComment();
                //session.setAttribute("event", eh.getEvent());
                //session.setAttribute("locat")
                response.sendRedirect("viewevent.jsp");
                //stub complete
            }
            else if(action.equals("removeFromCalendar")){ 
                CalendarHandler eh = new CalendarHandler(request);
                //eh.addEventToCalendar();
                eh.removeEventFromCalendar();
                session.setAttribute("event", eh.getEvent());
                session.setAttribute("userProfile", eh.getUserProfile());
                session.setAttribute("location", eh.getEvent().getLocation());
                //session.setAttribute("userProfile", scdb);
                if(action.equals("removeFromCalendar"))
                    response.sendRedirect("viewevent.jsp");
                else
                    response.sendRedirect("calendar.jsp");
            }
            
            else if(action.equals("Remove From Calendar")){
                CalendarHandler eh = new CalendarHandler(request);
                //eh.addEventToCalendar();
                eh.removeEventFromCalendar();
                session.setAttribute("event", eh.getEvent());
                session.setAttribute("userProfile", eh.getUserProfile());
                session.setAttribute("location", eh.getEvent().getLocation());
                //session.setAttribute("userProfile", scdb);
                if(action.equals("removeFromCalendar"))
                    response.sendRedirect("viewevent.jsp");
                else
                    response.sendRedirect("calendar.jsp");    
            }
            else if(action.equals("Remove Past Event")){
                CalendarHandler eh = new CalendarHandler(request);
                //eh.addEventToCalendar();
                eh.removeEventFromCalendar();
                session.setAttribute("event", eh.getEvent());
                session.setAttribute("userProfile", eh.getUserProfile());
                session.setAttribute("location", eh.getEvent().getLocation());
                //session.setAttribute("userProfile", scdb);
                if(action.equals("removeFromCalendar"))
                    response.sendRedirect("viewevent.jsp");
                else
                    response.sendRedirect("calendar.jsp");                    
            }
            
            else if(action.equals("search events")){
                CalendarHandler eh = new CalendarHandler(request);
                session.setAttribute("searchResults", eh.searchEvents());
                session.setAttribute("event", eh.getEvent());
                response.sendRedirect("searchevents.jsp");
            }
            
            else if(action.equals("advanced search events")){
                //todo
            }
            /**********************************
             *  Profile Options
             **********************************/
            
            else if(action.equals("profile")){
                ProfileHandler profileHandler = new ProfileHandler();
                String redirect = profileHandler.HandleProfileRequest(request,session);
                RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(redirect);
                dispatcher.forward(request, response);            
            }
            
            /************************************
             *  Group Options
             ************************************/
            
            else if(action.equals("group")){
                GroupHandler groupHandler = new GroupHandler();
                String redirect = groupHandler.handleGroupRequest(request,session);
                RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(redirect);
                dispatcher.forward(request, response);
            }
            
		//else{            
                //RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/index.jsp");
                //dispatcher.forward(request, response);
            //}
        
           /************************************
            *  Email Module
            ************************************/
            
            /**
             * Sends an email to all members
             */            
            else if (action.equals("Email Users")) {
		
                // Get email contents from writeMail.jsp
		String emailFrom = request.getParameter("from");
                session.setAttribute("emailFromN", emailFrom);
                String emailTitle = request.getParameter("title");
                session.setAttribute("emailTitleN", emailTitle);
                String emailBody = request.getParameter("body");
                session.setAttribute("emailBodyN", emailBody);
                String emailSign = request.getParameter("sign");
                session.setAttribute("emailSignN", emailSign);

                RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/sendMail.jsp");
                dispatcher.forward(request, response);
                }
            
            /**
             * Send an event reminder to members who signed up for the upcoming event
             */
            else if (action.equals("Send Reminders")) {
	        
                // Get upcoming event IDs that were selected by administrator 
                String[] evtIDs = request.getParameterValues("evtID");
                session.setAttribute("evtIDs", evtIDs);
                RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/statusOfSendingEmailReminders.jsp");
                dispatcher.forward(request, response);
                }
                
	}
        //jdbcon.close();
           
    }   
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
